package com.shenyue.nbtownlet.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.databinding.HoldListItemBinding;
import com.shenyue.nbtownlet.util.StringUtil;

public class HoldListViewAdapter extends BaseAdapter {
    private Context mContext;
    private JSONArray items = new JSONArray();

    public interface HoldListItemClickListener {
        void onItemClick(int position);
    }

    private HoldListItemClickListener listener;

    public HoldListViewAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (convertView == null) {
            HoldListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.hold_list_item, viewGroup, false);
            convertView = binding.getRoot();
            viewHolder = new ViewHolder(binding);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        HoldListItemBinding root = viewHolder.getBinding();
        root.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.onItemClick(position);
            }
        });
        JSONObject item = items.getJSONObject(position);
        root.taskcode.setText(StringUtil.getValue(item.getString("CODE")));
        root.TITILE.setText(StringUtil.getValue(item.getString("TITILE")));
        String CREATETIME = StringUtil.getValue(item.getString("CREATETIME"));
        if (CREATETIME.contains("T"))
            CREATETIME = CREATETIME.substring(0, CREATETIME.indexOf("T"));
        root.CREATETIME.setText(CREATETIME);
        root.CREATORNAME.setText(StringUtil.getValue(item.getString("CREATORNAME")));
        root.distance.setText(StringUtil.getValue(item.getString("distance")));

        return convertView;
    }

    public void addItems(JSONArray array) {
        items.addAll(array);
        notifyDataSetChanged();
    }

    public void clear() {
        items.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(HoldListItemClickListener listener) {
        this.listener = listener;
    }

    private class ViewHolder {
        private HoldListItemBinding binding;

        public ViewHolder(HoldListItemBinding binding) {
            this.binding = binding;
        }

        public HoldListItemBinding getBinding() {
            return binding;
        }
    }
}
